package device;

import control.options.IManageData;
import environment.data.BaseEnvironmentData;
import environment.object.BaseEnvironmentObject;
import org.w3c.dom.Element;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;


public class Conditioner extends BaseDevice implements IManageData,IDevice,IXmlConfig {


    private Long newValue;
    private Random rnd  = new Random();
    private Map <BaseEnvironmentData.EnvironmentData,BaseEnvironmentData.EnvironmentOptions> todo = new ConcurrentHashMap<BaseEnvironmentData.EnvironmentData, BaseEnvironmentData.EnvironmentOptions>();

    private String idXml;
    private Long minTemperature;
    private Long maxTemperature;


    public Conditioner(Long id, String name, BaseEnvironmentObject object,Long newValue) {
        super(id, name, object);
        this.newValue = newValue;
    }

    public Conditioner(Long id,String xmlId, String name, BaseEnvironmentObject object,Long newValue) {
        super(id, name, object);
        this.newValue = newValue;
        this.idXml = xmlId;
    }


    @Override
    public void run() {
        for (;;){
            updateData();
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void updateData() {
        Long rndValue = (long) rnd.nextInt(newValue.intValue());
        //System.out.println(System.currentTimeMillis()+"| "+ getName()+" "+ type.toString()+" "+rndValue);
        getEnvironmentObject().getListEnvironmentData().get(0).updateValue(rndValue, BaseEnvironmentData.EnvironmentOptions.MINUS);
       /* if (todo.size() != 0) {
            for (BaseEnvironmentData data : getEnvironmentObject().getListEnvironmentData()) {
                for (BaseEnvironmentData.EnvironmentData envData : todo.keySet()) {
                    if (data.getType() == envData) {
                        switch (data.getType()) {
                            case TEMPERATURE:
                                updateTemperature(data, todo.get(envData));
                                break;
                        }
                    }
                }

            }
        }*/

    }

    @Override
    public void updateTodo(BaseEnvironmentData.EnvironmentData data, BaseEnvironmentData.EnvironmentOptions options) {

    }

    private void updateTemperature(BaseEnvironmentData data,BaseEnvironmentData.EnvironmentOptions options){
        Long rndValue = (long)rnd.nextInt(newValue.intValue());
        /*if(data.getValue() >= maxTemperature){
            data.updateValue(rndValue, BaseEnvironmentData.EnvironmentOptions.MINUS);
            System.out.println(System.currentTimeMillis()+"| "+ getName()+" MINUS "+rndValue);
        } else if(data.getValue() <= minTemperature){
            data.updateValue(rndValue, BaseEnvironmentData.EnvironmentOptions.PLUS);
            System.out.println(System.currentTimeMillis()+"| "+ getName()+" PLUS "+rndValue);
        } else {
            System.out.println(System.currentTimeMillis()+"| "+ getName()+" wait");
        }*/switch (options){
            case MINUS:
                minusData(data,rndValue);
                break;
            case PLUS:
                plusData(data,rndValue);
                break;
        }

    }

    public Long getNewValue() {
        return newValue;
    }

    public void setNewValue(Long newValue) {
        this.newValue = newValue;
    }


    @Override
    public void plusData(BaseEnvironmentData data,Long value) {
        data.updateValue(value, BaseEnvironmentData.EnvironmentOptions.PLUS);
        System.out.println(System.currentTimeMillis()+"| "+ getName()+" PLUS "+value);
    }

    @Override
    public void minusData(BaseEnvironmentData data,Long value) {
        data.updateValue(value, BaseEnvironmentData.EnvironmentOptions.MINUS);
        System.out.println(System.currentTimeMillis()+"| "+ getName()+" MINUS "+value);
    }

    @Override
    public void setData(BaseEnvironmentData data,Long value) {

    }


    public Map<BaseEnvironmentData.EnvironmentData, BaseEnvironmentData.EnvironmentOptions> getTodo() {
        return todo;
    }

    public void setTodo(Map<BaseEnvironmentData.EnvironmentData, BaseEnvironmentData.EnvironmentOptions> todo) {
        this.todo = todo;
    }

    @Override
    public String getIdFromXml() {
        return idXml;
    }

    @Override
    public void initFromXml(Element element) {
        this.minTemperature  = Long.parseLong(element.getElementsByTagName("minTemperature").item(0).getChildNodes().item(0).getNodeValue());
        this.maxTemperature  = Long.parseLong(element.getElementsByTagName("maxTemperature").item(0).getChildNodes().item(0).getNodeValue());
        System.out.println(/*this.getName()+ */" min: " + minTemperature);
        System.out.println(/*this.getName()+*/" max: " + maxTemperature);
    }
}
